use "${datadir}\HTB_main", replace	

*------------------------------------------------------------------------------*
* Variables *
*------------------------------------------------------------------------------*
	

*--- Keep only non HTB
keep if htb_dummy==0


gen 		band_id = 1 if LTV<=60
replace 	band_id = 2 if LTV>60 & LTV<=70
replace 	band_id = 3 if LTV>70 & LTV<=75
replace 	band_id = 4 if LTV>75 & LTV<=80
replace 	band_id = 5 if LTV>80 & LTV<=85
replace 	band_id = 6 if LTV>85 & LTV<=90
replace 	band_id = 7 if LTV>90 & LTV<=95
label 		define band_idl 1 "40<ltv<=60" 2 "60<ltv<=70" 3 "70<ltv<=75" 4 "75<ltv<=80" 5 "80<ltv<=85" 6 "85<ltv<=90" 7 "90<ltv<=95"
label 		values band_id band_idl
tab 		band_id

gen LTV_round_counter = LTV_round
replace LTV_round_counter = LTV_round_counter - 20
replace LTV_round_counter = LTV_round_counter - 20 if htb_london

gen 		band_id_counter = 1 if LTV_round_counter<=60
replace 	band_id_counter = 2 if LTV_round_counter>60 & LTV_round_counter<=70
replace 	band_id_counter = 3 if LTV_round_counter>70 & LTV_round_counter<=75
replace 	band_id_counter = 4 if LTV_round_counter>75 & LTV_round_counter<=80
replace 	band_id_counter = 5 if LTV_round_counter>80 & LTV_round_counter<=85
replace 	band_id_counter = 6 if LTV_round_counter>85 & LTV_round_counter<=90
replace 	band_id_counter = 7 if LTV_round_counter>90 & LTV_round_counter<=95
label 		define band_id_counterl 1 "40<ltv<=60" 2 "60<ltv<=70" 3 "70<ltv<=75" 4 "75<ltv<=80" 5 "80<ltv<=85" 6 "85<ltv<=90" 7 "90<ltv<=95"
label 		values band_id_counter band_id_counterl
tab 		band_id_counter


gen APR = interest + 100 * lenderfees / 150000


gen band_large = .
replace band_large = 1 if band_id <=3
replace band_large = 2 if band_id >3 & band_id <=5
replace band_large = 3 if band_id >5

gen zerofee = (lenderfees == 0)


gen feeband = .
replace feeband = 1 if lenderfees == 0
replace feeband = 2 if lenderfees > 0 & lenderfees <= 550
replace feeband = 3 if lenderfees > 550 & lenderfees <= 1100
replace feeband = 4 if lenderfees > 1100



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Counterfactual Rate *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*


preserve
collapse (p50) interest interest_htb APR, by(advtype dealtype lender_id band_id yearm_date) 
rename interest interest_counter
rename APR APR_counter
rename band_id band_id_counter
sort advtype dealtype lender_id band_id_counter yearm_date 
save "${datadir}\counterfactual_rate", replace 
restore

preserve
collapse (p50) interest interest_htb, by(advtype dealtype lender_id band_id yearm_date feeband) 
rename interest interest_fee_counter
rename band_id band_id_counter
sort advtype dealtype lender_id band_id_counter yearm_date 
save "${datadir}\counterfactual_rate_zerofee", replace 
restore


merge m:1 advtype dealtype lender_id band_id_counter yearm_date using /// 
	"${datadir}\counterfactual_rate", keep(1 3) nogen

merge m:1 advtype dealtype lender_id band_id_counter yearm_date feeband using /// 
	"${datadir}\counterfactual_rate_zerofee", keep(1 3) nogen
	
	
gen interest_diff 		= interest 	-  interest_counter
gen APR_diff 			= APR 		-  APR_counter
gen interest_fee_diff 	= interest 	-  interest_fee_counter
sum interest_diff interest_fee_diff APR_diff, d

				
gen 		interest_counter00 	= interest_counter/100
gen 		loan_val_counter 	= (LTV - 20) / 100 * property_val
replace 	loan_val_counter 	= (LTV - 40) / 100 * property_val if htb_london


gen 		payment_counter		= loan_val_counter * ( interest_counter00/12 * (1+interest_counter00/12)^(mortgage_term * 12) ) / ((1+interest_counter00/12)^(mortgage_term * 12) - 1) // if fee_addedtoloan==0 // / 1000 				

gen 		payment_counter_rate= loan_val * ( interest_counter00/12 * (1+interest_counter00/12)^(mortgage_term * 12) ) / ((1+interest_counter00/12)^(mortgage_term * 12) - 1) // if fee_addedtoloan==0 // / 1000 				
gen 		payment_counter_loan= loan_val_counter * ( interest00/12 * (1+interest00/12)^(mortgage_term * 12) ) / ((1+interest00/12)^(mortgage_term * 12) - 1) // if fee_addedtoloan==0 // / 1000 				



*------------------------------------------------------------------------------*
* Compute *
*------------------------------------------------------------------------------*

/* 1) House Price (hp) */
gen dhp = (fw_appreciation2+1)^(1/2) -1
replace dhp = dhp * 100

gen dhp_bw = (bw_appreciation2+1)^(1/2) -1
replace dhp_bw = dhp_bw * 100


gen hpa_noHTB_round = property_val * fw_appreciation2 
gen hpa_HTB_round 	= cond(htb_london, 0.6 * property_val * fw_appreciation2, ///
	0.8 * property_val * fw_appreciation2)
gen hpa_diff_round 	= hpa_HTB_round - hpa_noHTB_round 

gen hpv_noHTB_round = property_val * (1 + fw_appreciation2) 
gen hpv_HTB_round 	= cond(htb_london, 0.6 *  property_val * (1 + fw_appreciation2), ///
	0.8 *  property_val * (1 + fw_appreciation2))
gen hpv_diff_round 	= hpv_HTB_round - hpv_noHTB_round 



/* 2) Monthly payment (mp)  */

gen delta_deposit	= 0.1 / (100 * 12 ) 		// 
gen delta_mortgage	= interest / (100 * 12 ) 	// 
gen delta_credcard 	= 20 /(100 * 12 ) 			// 


gen mp_noHTB_round 	= payment
gen mp_HTB_round 	= payment_counter

gen mp_diff_round 	= mp_HTB_round - mp_noHTB_round

foreach type in mortgage credcard { //  
	foreach group in noHTB HTB diff {
		gen fvmp_`type'_`group'_round = mp_`group'_round * ( (1+delta_`type')^(dealtype*12) - 1 ) / delta_`type' 
	}
}


/* 3) Left balance (lb) */
gen lb_noHTB_round 	= loan_val * (1+interest00/12)^(dealtype*12) ///
					-  payment * (((1+interest00/12)^(dealtype*12) - 1)/(interest00/12) ) 

gen lb_HTB_round 	= loan_val_counter * (1+interest_counter00/12)^(dealtype*12) ///
					-  payment_counter * (((1+interest_counter00/12)^(dealtype*12) - 1)/(interest_counter00/12) ) 

gen lb_diff_round 	= lb_HTB_round - lb_noHTB_round // (1+appreciation2)
					

/* 4) Full = 1) + 2) + 3) */

gen full_mortgage_noHTB_round 	= hpv_noHTB_round - fvmp_mortgage_noHTB_round - lb_noHTB_round
gen full_mortgage_HTB_round 	= hpv_HTB_round - fvmp_mortgage_HTB_round - lb_HTB_round
gen full_mortgage_diff_round 	= (hpv_HTB_round - fvmp_mortgage_HTB_round - lb_HTB_round) ///
								- (hpv_noHTB_round - fvmp_mortgage_noHTB_round - lb_noHTB_round)

gen full_credcard_noHTB_round 	= hpv_noHTB_round - fvmp_credcard_noHTB_round - lb_noHTB_round
gen full_credcard_HTB_round 	= hpv_HTB_round - fvmp_credcard_HTB_round - lb_HTB_round
gen full_credcard_diff_round 	= (hpv_HTB_round - fvmp_credcard_HTB_round - lb_HTB_round) ///
								- (hpv_noHTB_round - fvmp_credcard_noHTB_round - lb_noHTB_round)


								
/* 5) Breakeven house price */

gen bkhp_mortgage = ( ( (fvmp_mortgage_noHTB_round + lb_noHTB_round) ///
						- (fvmp_mortgage_HTB_round + lb_HTB_round) ) / ///
							( 0.2 * property_val) )^(1/dealtype) - 1
replace bkhp_mortgage = ( ( (fvmp_mortgage_noHTB_round + lb_noHTB_round) ///
						- (fvmp_mortgage_HTB_round + lb_HTB_round) ) / ///
							( 0.4 * property_val) )^(1/dealtype) - 1 if htb_london
replace bkhp_mortgage = bkhp_mortgage * 100


gen bkhp_mortgage_credcard = ( ( (fvmp_credcard_noHTB_round + lb_noHTB_round) ///
						- (fvmp_credcard_HTB_round + lb_HTB_round) ) / ///
							( 0.2 * property_val) )^(1/dealtype) - 1
replace bkhp_mortgage_credcard = ( ( (fvmp_credcard_noHTB_round + lb_noHTB_round) ///
						- (fvmp_credcard_HTB_round + lb_HTB_round) ) / ///
							( 0.4 * property_val) )^(1/dealtype) - 1 if htb_london
replace bkhp_mortgage_credcard = bkhp_mortgage_credcard * 100



so _all
save "${datadir}\HTB_main_cleaned_counterfactual", replace

keep if subsample
save "${datadir}\HTB_main_cleaned_counterfactual_sub", replace